package com.umeox.watch.moto.contact.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.umeox.moto.common.log.Logger;
import com.umeox.watch.moto.contact.utils.Utils;
import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.dataservice.constants.Constants;

public class PhoneStateReceiver extends BroadcastReceiver {
    private static final String ACTION_SHUTDOWN = "android.intent.action.ACTION_SHUTDOWN";

    private static PhoneStateReceiver receiver;

    private PhoneStateListener phoneStateListener;

    private static PhoneStateReceiver getReceiver() {
        if (receiver == null) {
            synchronized (PhoneStateReceiver.class) {
                if (receiver == null) {
                    receiver = new PhoneStateReceiver();
                }
            }
        }
        return receiver;
    }

    public static void registerPhoneStateReceiver(Context context, PhoneStateListener phoneStateListener) {
        unRegisterPhoneStateReceiver(context);

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
        intentFilter.addAction(Constants.ACTION_CALL_PHONE);//收到远程监听广播
        intentFilter.addAction(Constants.ACTION_POWEROFF);//关机广播
        intentFilter.addAction(ACTION_SHUTDOWN);//系统关机广播
        context.registerReceiver(getReceiver(), intentFilter);
        getReceiver().setPhoneStateListener(phoneStateListener);
    }

    public static void unRegisterPhoneStateReceiver(Context context) {
        try {
            context.unregisterReceiver(getReceiver());
        } catch (Exception ignored) {
        }
    }

    public void setPhoneStateListener(PhoneStateListener phoneStateListener) {
        this.phoneStateListener = phoneStateListener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Logger.w("PhoneStateReceiver: " + action);
        if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equalsIgnoreCase(action)) {
            TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            int state = telephony.getCallState();
            if (state == TelephonyManager.CALL_STATE_RINGING) {
                String phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                String contactName = Utils.getContactNameFromPhoneBook(context,phoneNumber);
                Logger.e("phoneNumber=" + phoneNumber + ",contactName=" + contactName);
                if(DataProvider.isRefusePhone() && TextUtils.isEmpty(contactName)){ //防止白名单开启时陌生人来电打断正在进行的通话
                    return;
                }
                if (this.phoneStateListener != null) {
                    this.phoneStateListener.onCallStateRinging();
                }
            } else if (state == TelephonyManager.CALL_STATE_IDLE) {
            } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
            }
        } else if (Constants.ACTION_CALL_PHONE.equalsIgnoreCase(action)) {
            Logger.w("收到远程监听广播，挂断视频通话");
            if (this.phoneStateListener != null) {
                this.phoneStateListener.onCallStateRinging();
            }
        } else if (ACTION_SHUTDOWN.equalsIgnoreCase(action)
                || Constants.ACTION_POWEROFF.equalsIgnoreCase(action)) {
            Logger.w("收到关机广播，挂断视频通话");
            if (this.phoneStateListener != null) {
                this.phoneStateListener.onShutdown();
            }
        }
    }

    public interface PhoneStateListener {
        /**
         * Callback invoked when device call state changes.
         *
         * @see TelephonyManager#CALL_STATE_IDLE
         * @see TelephonyManager#CALL_STATE_RINGING
         * @see TelephonyManager#CALL_STATE_OFFHOOK
         */
        void onCallStateRinging();

        void onShutdown();
    }
}
